import getGoodsInfoById from 'services/goodsInfo'

const types = {
  GET_GOODSINFO_BY_ID_PENDING: 'GET_GOODSINFO_BY_ID_PENDING',
  GET_GOODSINFO_BY_ID_SUCCESS: 'GET_GOODSINFO_BY_ID_SUCCESS',
  GET_GOODSINFO_BY_ID_FAILURE: ' GET_GOODSINFO_BY_ID_FAILURE',
}

const state = {
  goodsRow: {},
  loading: false,
  goodsImgs: [],
}

const actions = {
  getGoodsInfo({ commit }, payload) {
    commit(types.GET_GOODSINFO_BY_ID_PENDING)
    getGoodsInfoById(payload)
      .then((res) => {
        commit(types.GET_GOODSINFO_BY_ID_SUCCESS, res)
      })
  },
}

const mutations = {
  [types.GET_GOODSINFO_BY_ID_PENDING](state) {
    state.loading = true
  },

  [types.GET_GOODSINFO_BY_ID_SUCCESS](state, res) {
    state.goodsRow = res.data
    state.goodsImgs = res.data.imageList.length
      ? res.data.imageList.map(item => item.imageUrl)
      : []
    // console.log(state.goodsImgs)
    state.loading = false
  },

  [types.GET_GOODSINFO_BY_ID_FAILURE](state, res) {
    state.goodsRow = res.info
    state.loading = false
  },
}

export default {
  state,
  actions,
  mutations,
}
